//
//  TravelDetailCell.m
//  XhlTrip
//
//  Created by dllo on 15/8/22.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TravelDetailCell.h"
#import "TravelDetailCollectionCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+WebP.h"


@implementation TravelDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.diction = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)passValue:(NSDictionary *)dic
{
        [self.diction setDictionary:dic];
        [self.imageView removeFromSuperview];
        [self.collectionView removeFromSuperview];
        [self createImageView];
        [self.mageView sd_setImageWithURL:[[dic objectForKey:@"photo"] objectForKey:@"url"]];
   
        [self createCollection];
    
}

/* 一个cell 创建collection */
- (void)createCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.contentView addSubview:self.collectionView];
    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor clearColor];
    /* 重用池 */
    static NSString *re = @"ff";
    [self.collectionView registerClass:[TravelDetailCollectionCell class] forCellWithReuseIdentifier:re];
    [_collectionView release];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}


/* collection 上的desLabel的文字 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TravelDetailCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ff" forIndexPath:indexPath];
      cell.desLabel.text = [self.diction objectForKey:@"description"];
    return cell;
}




/* 每一个cell 的大小 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat heightDes = [[self class] heightWithText:[self.diction objectForKey:@"description"] width:self.contentView.frame.size.width - 20 * Width];
    CGSize ff = CGSizeMake(collectionView.frame.size.width, heightDes);
    return ff;
}



- (void)createImageView
{
    self.mageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.mageView];
    [_mageView release];
}

#pragma mark - 铺视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if ([self.diction objectForKey:@"photo"]) {
        CGFloat photoHeight = [[self class] heightWithImage:[self.diction objectForKey:@"photo"] width:self.contentView.frame.size.width - 20 * Width];
        self.mageView.frame = CGRectMake(10 * Width, 5 * Height, self.contentView.frame.size.width - 20 * Width, photoHeight);
    }else
    {
        self.mageView.frame = CGRectMake(0, 0, 0, 0);
    }
    
    if ([[self.diction objectForKey:@"description"] isKindOfClass:[NSNull class]]) {
        
        self.collectionView.frame = CGRectMake(0, 0, 0, 0);
        
    }else{
        CGFloat heightDes = [[self class] heightWithText:[self.diction objectForKey:@"description"] width:self.contentView.frame.size.width - 20 * Width];
        self.collectionView.frame = CGRectMake(10 * Width, self.mageView.frame.size.height + 15 * Height, self.contentView.frame.size.width - 20 * Width, heightDes);
        
        
    }
}

#pragma mark - 文字高度自适应
+ (CGFloat)heightWithText:(NSString *)text width:(CGFloat )width
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Verdana-Italic" size:17 * Width]} context:nil];
    return rect.size.height;
}

#pragma mark - 图片高度自适应
+ (CGFloat)heightWithImage:(NSDictionary *)magedic width:(CGFloat)width
{
    /* 等比例缩放算出高度 */
    CGFloat newh = [[magedic objectForKey:@"image_height"] floatValue] / [[magedic objectForKey:@"image_width"] floatValue] * width;
    /* 返回高度 */
    return newh;
    
}

- (void)dealloc
{
    [_diction release];
    [_mageView release];
    [_namelabel release];
    [_collectionView release];
    [super dealloc];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
